package com.shuiyun.jtt.message;

import com.shuiyun.jtt.enums.Constant;
import com.shuiyun.jtt.utils.codec.DataType;
import com.shuiyun.jtt.utils.codec.FieldValue;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author shuiyun
 * @description 头信息
 * @date 2022-04-18 20:09
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Header implements BaseHeader{


    /**
     * 消息类型
     */
    @FieldValue(type = DataType.WORD,desc = "消息ID")
    protected int messageId;

    /**
     * 消息体属性
     */
    @FieldValue(type = DataType.WORD,desc = "消息体属性")
    protected int properties;

    /**
     * 协议版本号
     */
    @FieldValue(type = DataType.BYTE,desc = "协议版本号")
    protected int versionNo;

    /**
     * 手机号
     */
    @FieldValue(type = DataType.BCD8421,length = 10,desc = "终端手机号")
    protected String mobileNo;

    /**
     * 消息序列号
     */
    @FieldValue(type = DataType.WORD,desc = "消息流水号")
    protected int serialNo;

    /**
     * 包总数
     */
    @FieldValue(type = DataType.WORD,desc = "包总数",checkMethod = {"properties","&!","2000"})
    protected Integer packageTotal;

    /**
     * 包序号
     */
    @FieldValue(type = DataType.WORD,desc = "包编号",checkMethod = {"properties","&!","2000"})
    protected Integer packageNo;

    /**
     * bcc校验
     */
    private boolean verified = true;

    @Override
    public String getClientId() {
        return mobileNo;
    }

    @Override
    public Object getType() {
        return messageId;
    }

    /**
     * 消息体长度
     */
    public int getBodyLength() {
        return this.properties & Constant.BODY_LENGTH;
    }

    public void setBodyLength(int bodyLength) {
        this.properties ^= (properties & Constant.BODY_LENGTH);
        this.properties |= bodyLength;
    }

    /**
     * 加密方式
     */
    public int getEncryption() {
        return (properties & Constant.ENCRYPTION) >> 10;
    }

    public void setEncryption(int encryption) {
        this.properties ^= (properties & Constant.ENCRYPTION);
        this.properties |= (encryption << 10);
    }

    /**
     * 是否分包
     */
    public boolean isSubpackage() {
        return (properties & Constant.SUBPACKAGE) == Constant.SUBPACKAGE;
    }

    public void setSubpackage(boolean subpackage) {
        if (subpackage) {
            this.properties |= Constant.SUBPACKAGE;
        } else {
            this.properties ^= (properties & Constant.SUBPACKAGE);
        }
    }

    /**
     * 是否有版本
     */
    public boolean isVersion() {
        return (properties & Constant.VERSION) == Constant.VERSION;
    }

    public void setVersion(boolean version) {
        if (version) {
            this.properties |= Constant.VERSION;
        } else {
            this.properties ^= (properties & Constant.VERSION);
        }
    }

    /**
     * 保留位
     */
    public boolean isReserved() {
        return (properties & Constant.RESERVED) == Constant.RESERVED;
    }

    public void setReserved(boolean reserved) {
        if (reserved) {
            this.properties |= Constant.RESERVED;
        } else {
            this.properties ^= (properties & Constant.RESERVED);
        }
    }

    public Header(int messageId, int serialNo, String mobileNo) {
        this.messageId = messageId;
        this.serialNo = serialNo;
        this.mobileNo = mobileNo;
        this.versionNo = 1;
        this.setVersion(true);
    }



}
